package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.DTO.truck.TruckTypeDto;
import com.itheima.pinda.DTO.user.TruckDriverDto;
import com.itheima.pinda.common.CustomIdGenerator;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.entity.PdTruckType;
import com.itheima.pinda.entity.PdTruckTypeGoodsType;
import com.itheima.pinda.mapper.PdTruckTypeGoodsTypeMapper;
import com.itheima.pinda.mapper.PdTruckTypeMapper;
import com.itheima.pinda.service.PdTruckTypeGoodsTypeService;
import com.itheima.pinda.service.PdTruckTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PdTruckTypeServiceImpl implements PdTruckTypeService {

    @Autowired
    private CustomIdGenerator idGenerator;
    @Autowired
    private PdTruckTypeMapper truckTypeMapper;
    @Autowired
    private PdTruckTypeGoodsTypeService truckTypeGoodsTypeService;
    @Autowired
    private PdTruckTypeGoodsTypeMapper truckTypeGoodsTypeMapper;

    @Override
    @Transactional
    public TruckTypeDto truckTypeInsert(TruckTypeDto dto) {

        PdTruckType pdTruckType = new PdTruckType();
        BeanUtils.copyProperties(dto,pdTruckType);

        if (StringUtils.isEmpty(pdTruckType.getId())) {
            // 新增
            pdTruckType.setId(idGenerator.nextId(pdTruckType) + "");

            truckTypeMapper.insert(pdTruckType);

        }else {
            // 修改
            truckTypeMapper.updateById(pdTruckType);
            // 删除关联关系
            truckTypeGoodsTypeMapper.delete(new LambdaQueryWrapper<PdTruckTypeGoodsType>()
                    .eq(PdTruckTypeGoodsType::getTruckTypeId, dto.getId()));
        }

        // 新增车辆类型与货物类型关系
        if (!ObjectUtils.isEmpty(dto.getGoodsTypeIds())) {
            List<String> goodsTypeIds = dto.getGoodsTypeIds();

            List<PdTruckTypeGoodsType> list = goodsTypeIds.stream().map(goodsTypeId -> {
                PdTruckTypeGoodsType pdTruckTypeGoodsType = new PdTruckTypeGoodsType();
                pdTruckTypeGoodsType.setId(idGenerator.nextId(pdTruckTypeGoodsType) + "");
                pdTruckTypeGoodsType.setTruckTypeId(pdTruckType.getId());
                pdTruckTypeGoodsType.setGoodsTypeId(goodsTypeId);
                return pdTruckTypeGoodsType;
            }).collect(Collectors.toList());

            truckTypeGoodsTypeService.AddList(list);
        }

        dto.setId(pdTruckType.getId());
        return dto;
    }

    @Override
    public TruckTypeDto selectById(String id) {
        PdTruckType pdTruckType = truckTypeMapper.selectById(id);
        TruckTypeDto truckTypeDto = new TruckTypeDto();
        BeanUtils.copyProperties(pdTruckType,truckTypeDto);

        // 查询关联关系
        List<PdTruckTypeGoodsType> pdTruckTypeGoodsTypes =
                truckTypeGoodsTypeMapper.selectList(
                        new LambdaQueryWrapper<PdTruckTypeGoodsType>()
                        .eq(PdTruckTypeGoodsType::getTruckTypeId, truckTypeDto.getId()));
        List<String> list = pdTruckTypeGoodsTypes.stream().map(PdTruckTypeGoodsType::getGoodsTypeId).collect(Collectors.toList());
        truckTypeDto.setGoodsTypeIds(list);
        return truckTypeDto;
    }

    @Override
    public IPage<TruckTypeDto> selectByPage(Integer pageNum, Integer pageSize, String name, BigDecimal allowableLoad, BigDecimal allowableVolume) {
        Page<PdTruckType> page = new Page<>(pageNum, pageSize);
        Page<PdTruckType> pdTruckTypePage = truckTypeMapper.selectPage(page, new LambdaQueryWrapper<PdTruckType>()
                .eq(!StringUtils.isEmpty(name),PdTruckType::getName, name)
                .eq(!StringUtils.isEmpty(allowableLoad),PdTruckType::getAllowableLoad, allowableLoad)
                .eq(!StringUtils.isEmpty(allowableVolume),PdTruckType::getAllowableVolume, allowableVolume));


        ArrayList<TruckTypeDto> truckTypeDtos = new ArrayList<>();

        List<PdTruckType> pdTruckTypes = pdTruckTypePage.getRecords();

        // 查询关联关系信息
        for (PdTruckType pdTruckType : pdTruckTypes) {

            TruckTypeDto truckTypeDto = new TruckTypeDto();
            List<PdTruckTypeGoodsType> pdTruckTypeGoodsTypes =
                    truckTypeGoodsTypeMapper.selectList(
                            new LambdaQueryWrapper<PdTruckTypeGoodsType>()
                                    .eq(PdTruckTypeGoodsType::getTruckTypeId, pdTruckType.getId()));
            List<String> collect = pdTruckTypeGoodsTypes.stream().map(PdTruckTypeGoodsType::getGoodsTypeId).collect(Collectors.toList());

            BeanUtils.copyProperties(pdTruckType,truckTypeDto);
            truckTypeDto.setGoodsTypeIds(collect);
            truckTypeDtos.add(truckTypeDto);
        }

        return new Page<TruckTypeDto>().setRecords(truckTypeDtos);
    }

    @Override
    public void truckTypeDelete(List<String> ids) {
        truckTypeMapper.update(null,
                new LambdaUpdateWrapper<PdTruckType>()
                        .set(PdTruckType::getStatus, Constant.DATA_DISABLE_STATUS)
                        .in(PdTruckType::getId,ids));
    }


}
